package com.jblv.frame.common.es.service.impl;

import com.jblv.frame.common.es.service.IESIndexService;
import io.searchbox.client.JestClient;
import io.searchbox.client.JestResult;
import io.searchbox.cluster.Health;
import io.searchbox.cluster.NodesInfo;
import io.searchbox.cluster.NodesStats;
import io.searchbox.indices.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;

/**
 * @author happyonion.yangtao
 * @version 1.0
 * @title ESIndexService
 * @projectName jblv-frame
 * @packageName com.jblv.common.es.service.impl
 * @description es
 * @date Create at 2020/4/29 17:31
 * @since jdk1.8
 */

@Service
public class ESIndexService implements IESIndexService {
    private static Logger logger = LoggerFactory.getLogger(ESIndexService.class);
    @Autowired
    private JestClient jestClient;

    public ESIndexService() {
    }

    @Override
    public JestResult deleteIndex(String type) {
        DeleteIndex deleteIndex = (new DeleteIndex.Builder(type)).build();
        Object result = null;

        try {
            this.jestClient.execute(deleteIndex);
            logger.info("deleteIndex == " + ((JestResult)result).getJsonString());
        } catch (IOException var5) {
            var5.printStackTrace();
        }

        return (JestResult)result;
    }

    @Override
    public JestResult clearCache() {
        ClearCache closeIndex = (new io.searchbox.indices.ClearCache.Builder()).build();
        Object result = null;

        try {
            this.jestClient.execute(closeIndex);
            logger.info("clearCache == " + ((JestResult)result).getJsonString());
        } catch (IOException var4) {
            var4.printStackTrace();
        }

        return (JestResult)result;
    }

    @Override
    public JestResult closeIndex(String type) {
        CloseIndex closeIndex = (new io.searchbox.indices.CloseIndex.Builder(type)).build();
        Object result = null;

        try {
            this.jestClient.execute(closeIndex);
            logger.info("closeIndex == " + ((JestResult)result).getJsonString());
        } catch (IOException var5) {
            var5.printStackTrace();
        }

        return (JestResult)result;
    }

    @Override
    public JestResult optimizeIndex() {
        Optimize optimize = (new io.searchbox.indices.Optimize.Builder()).build();
        Object result = null;

        try {
            this.jestClient.execute(optimize);
            logger.info("optimizeIndex == " + ((JestResult)result).getJsonString());
        } catch (IOException var4) {
            var4.printStackTrace();
        }

        return (JestResult)result;
    }

    @Override
    public JestResult flushIndex() {
        Flush flush = (new io.searchbox.indices.Flush.Builder()).build();
        Object result = null;

        try {
            this.jestClient.execute(flush);
            logger.info("flushIndex == " + ((JestResult)result).getJsonString());
        } catch (IOException var4) {
            var4.printStackTrace();
        }

        return (JestResult)result;
    }

    @Override
    public JestResult indicesExists() {
        IndicesExists indicesExists = (new io.searchbox.indices.IndicesExists.Builder("article")).build();
        Object result = null;

        try {
            this.jestClient.execute(indicesExists);
            logger.info("indicesExists == " + ((JestResult)result).getJsonString());
        } catch (IOException var4) {
            var4.printStackTrace();
        }

        return (JestResult)result;
    }

    @Override
    public JestResult nodesInfo() {
        NodesInfo nodesInfo = (new io.searchbox.cluster.NodesInfo.Builder()).build();
        Object result = null;

        try {
            this.jestClient.execute(nodesInfo);
            logger.info("nodesInfo == " + ((JestResult)result).getJsonString());
        } catch (IOException var4) {
            var4.printStackTrace();
        }

        return (JestResult)result;
    }

    @Override
    public JestResult health() {
        Health health = (new io.searchbox.cluster.Health.Builder()).build();
        Object result = null;

        try {
            this.jestClient.execute(health);
            logger.info("health == " + ((JestResult)result).getJsonString());
        } catch (IOException var4) {
            var4.printStackTrace();
        }

        return (JestResult)result;
    }

    @Override
    public JestResult nodesStats() {
        NodesStats nodesStats = (new io.searchbox.cluster.NodesStats.Builder()).build();
        Object result = null;

        try {
            this.jestClient.execute(nodesStats);
            logger.info("nodesStats == " + ((JestResult)result).getJsonString());
        } catch (IOException var4) {
            var4.printStackTrace();
        }

        return (JestResult)result;
    }
}
